import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ReggieApplication;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;

import java.util.List;
import java.util.Set;

@SpringBootTest(classes = ReggieApplication.class)
public class test {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void cs() {
//      redisTemplate.opsForValue().set("name","zhangsan");
//      redisTemplate.opsForValue().set("city","beijing");
        String name = (String) redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
    @Test
    void hash(){
        redisTemplate.opsForHash().put("user","name","zhangsan");
        redisTemplate.opsForHash().put("user","age","18");
        redisTemplate.opsForHash().put("user","sex","男");

        Object o = redisTemplate.opsForHash().get("user", "name");
        System.out.println(o);
        System.out.println("--------------------------------");

        Set user = redisTemplate.opsForHash().keys("user");
        for (Object s:user){
            System.out.println(s);
        }
    }

    @Test
    void  list(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","a");
        listOperations.leftPushAll("list","b","c","d");//后面的会插到最左边

        Object list = listOperations.index("list", 1);//索引从零开始
        System.out.println(list);
        System.out.println("--------------------------------");
        List list1 = listOperations.range("list", 0, 3);
        for (Object o:list1){
            System.out.println(o);
        }

    }
    @Test
    void set(){//无序set（不重复）
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set","a","b","c","d");

        Boolean member = setOperations.isMember("set", "c");//判断存不存在
        System.out.println(member);
        System.out.println("-----------------------------------");

        Set<String> set = setOperations.members("set");//遍历
        for (String s:set){
            System.out.println(s);
        }
    }
    @Test
    void zSet(){//有序不重复
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","a",1);
        zSetOperations.add("zset","b",2);
        zSetOperations.add("zset","c",2);
        zSetOperations.add("zset","d",3);

        Set<String> zset = zSetOperations.range("zset", 0, -1);
        for (String s:zset){
            System.out.println(s);
        }
        System.out.println("----------------------------------------");

        zSetOperations.incrementScore("zset","c",1.5);//修改分数改变序列注意这是在原有基础上加
        Set<String> zset1 = zSetOperations.range("zset", 0, -1);
        for (String s:zset1){
            System.out.println(s);
        }
        System.out.println("----------------------------------------");

        zSetOperations.remove("zset","c");//删除成员
        Set<String> zset2 = zSetOperations.range("zset", 0, -1);
        for (String s:zset2){
            System.out.println(s);
        }
        System.out.println("----------------------------------------");
    }

    @Test
    void ty(){//通用
        //获取所有key
        Set<String> keys = redisTemplate.keys("*");
        for (String s:keys){
            System.out.println(s);
        }
        //判断某个key是否存在 has是有的意思
        Boolean user = redisTemplate.hasKey("user");
        System.out.println(user);

        //删除指定key
        redisTemplate.delete("user");
        //判断某个key是否存在 has是有的意思
        Boolean user1 = redisTemplate.hasKey("user");
        System.out.println(user1);

        //获取指定key的value类型
        DataType zset = redisTemplate.type("zset");
        System.out.println(zset.name());
    }
}
